package com.ifonly.common;

import com.google.common.base.Strings;
import com.ifonly.render.MainRenderFactory;
import com.ifonly.render.factorys.FtlMainRenderFactory;
import com.ifonly.render.factorys.JspMainRenderFactory;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-11 09:32
 * @since JDK 1.6
 */
public enum ViewType {
    JSP(".jsp"),
    FTL(".ftl");

    public String extension;

    ViewType(String extension) {
        this.extension = extension;
    }

    public static ViewType get(String name) {
        if (Strings.isNullOrEmpty(name)) {
            return null;
        }

        if (name.equalsIgnoreCase(JSP.name())) {
            return JSP;
        } else if (name.equalsIgnoreCase(FTL.name())) {
            return FTL;
        }
        return null;
    }

    public static MainRenderFactory getMainRenderFactory(String name) {
        if (Strings.isNullOrEmpty(name)) {
            throw new RuntimeException("view type can not be null or empty");
        }

        if (name.equalsIgnoreCase(JSP.name())) {
            return new JspMainRenderFactory();
        } else if (name.equalsIgnoreCase(FTL.name())) {
            return new FtlMainRenderFactory();
        }

        throw new RuntimeException("no render for [" + name + "]");
    }
}
